package cn.itsource.basic.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.FastDfsApiOpr;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/file")
public class FastDfsController {

    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
        try {
            System.out.println(file);
            String filename = file.getOriginalFilename();
            //获取后缀名
            String extension = FilenameUtils.getExtension(filename);
            //上传附件（返回附件对应的url地址）
            String upload = FastDfsApiOpr.upload(file.getBytes(), extension);
            return new AjaxResult().setResultObj(upload);
        } catch (IOException e) {
            e.printStackTrace();
            return  new AjaxResult(false,e.getMessage());
        }
    }
}
